﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.project.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.dao.CompanyCcDao;
import com.hh.bss.customer.dao.CompanyCvDao;
import com.hh.bss.customer.dao.CompanyDao;
import com.hh.bss.customer.dao.CompanyDeptDao;
import com.hh.bss.customer.dao.CompanyDeptTitleDao;
import com.hh.bss.customer.entity.Company;
import com.hh.bss.customer.entity.CompanyCc;
import com.hh.bss.customer.entity.CompanyCv;
import com.hh.bss.customer.entity.CompanyDept;
import com.hh.bss.customer.entity.CompanyDeptTitle;
import com.hh.bss.customer.service.CompanyService;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.talent.dao.CvContactDetailDao;
import com.hh.bss.talent.dao.CvDao;
import com.hh.bss.talent.dao.CvSalaryDao;
import com.hh.bss.talent.entity.Cv;
import com.hh.bss.talent.entity.CvContactDetail;
import com.hh.bss.talent.entity.CvSalary;
import com.hh.bss.talent.service.CvService;
import com.hh.bss.util.*;




@Service
@Transactional
public class SearchProjectCcListServiceImpl extends BaseBusinessService<SearchProjectCcList,java.lang.Integer>  implements SearchProjectCcListService<SearchProjectCcList,java.lang.Integer>{

	private SearchProjectCcListDao searchProjectCcListDao;
	private CompanyCvDao companyCvDao;
	private CvDao cvDao;
	private CvService cvService;
	private CvSalaryDao cvSalaryDao;
	private CompanyDao companyDao;
	private CompanyDeptDao companyDeptDao;
	private CompanyDeptTitleDao companyDeptTitleDao;
	private CvContactDetailDao cvContactDetailDao;
	private SearchProjectService searchProjectService;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	
	private SearchProjectCvService searchProjectCvService;
	public void setSearchProjectCvServiceImpl(SearchProjectCvService service) {
		this.searchProjectCvService = service;
	}
	private CompanyService companyService;
	public void setCompanyServiceImpl(CompanyService service) {
		this.companyService = service;
	}
	public void setCvServiceImpl(CvService service) {
		this.cvService = service;
	}
	public void setCompanyCvDaoImpl(CompanyCvDao dao) {
		this.companyCvDao = dao;
	}
	public void setCvSalaryDaoImpl(CvSalaryDao dao) {
		this.cvSalaryDao = dao;
	}
	public void setCvDaoImpl(CvDao dao) {
		this.cvDao = dao;
	}
	public void setCompanyDaoImpl(CompanyDao dao) {
		this.companyDao = dao;
	}
	public void setCompanyDeptDaoImpl(CompanyDeptDao dao) {
		this.companyDeptDao = dao;
	}
	public void setCompanyDeptTitleDaoImpl(CompanyDeptTitleDao dao) {
		this.companyDeptTitleDao = dao;
	}
	public void setCvContactDetailDaoImpl(CvContactDetailDao dao) {
		this.cvContactDetailDao = dao;
	}
	public void setSearchProjectServiceImpl(SearchProjectService service) {
		this.searchProjectService = service;
	}
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setSearchProjectCcListDaoImpl(SearchProjectCcListDao dao) {
		this.searchProjectCcListDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.searchProjectCcListDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(SearchProjectCcListQuery query) {
		return searchProjectCcListDao.findPage(query);
	}
	
	
	
	public Page findCcPage(SearchProjectCcListQuery query){
		return searchProjectCcListDao.findCcPage(query);
	}
	
	public void saveCcCv(SearchProjectCcList searchProjectCcList,SearchProjectCv searchProjectCv,Cv cv,CvSalary cvSalary,List cvContacts){
		//如果没有cvId,则是没有资料的新人选
		if(!StringHelper.isEmpty(cv.getCvName())&&(cv.getId()==null||cv.getId().intValue()==0)){
			//如果选择的不是CC人选或者其它人选则新增简历
			//if(searchProjectCcList.getCcResult() == 2|| searchProjectCcList.getCcResult() == 3){
				try{
					//设置人选当前公司，部门,职位			
					if (searchProjectCv.getCompanyId()!=null&&searchProjectCv.getCompanyId().intValue()!=0) {
						Company company=(Company)companyDao.getById(searchProjectCv.getCompanyId());
						cv.setCompany(company.getAbbrName());
						cv.setCompanyId(searchProjectCv.getCompanyId());
					}
					if (searchProjectCv.getDeptId()!=null&&searchProjectCv.getDeptId().intValue()!=0) {
						CompanyDept dept=(CompanyDept)companyDeptDao.getById(searchProjectCv.getDeptId());
						cv.setDept(dept.getName());
					}
					if (searchProjectCv.getTitleId()!=null&&searchProjectCv.getTitleId().intValue()!=0) {
						CompanyDeptTitle title=(CompanyDeptTitle)companyDeptTitleDao.getById(searchProjectCv.getTitleId());
						cv.setTitle(title.getName());
					}
				}catch(Exception ex){
					ex.printStackTrace();
				}
				cvDao.save(cv);
				cvDao.flush();
				for(int i=0;cvContacts!=null&&i<cvContacts.size();i++){
					CvContactDetail cvContactDetail=(CvContactDetail)cvContacts.get(i);
					cvContactDetail.setCvId(cv.getId());
					cvContactDetailDao.save(cvContactDetail);
					cvContactDetailDao.flush();
				}
				//简历加入项目
				//SearchProjectCv searchProjectCv=new SearchProjectCv();
				searchProjectCv.setCreateDt(new Date());
				searchProjectCv.setCreator(searchProjectCcList.getCreator());
				searchProjectCv.setProjectId(searchProjectCcList.getProjectId());
				searchProjectCv.setCvId(cv.getId());
				
				cvContactDetailDao.flush();
				if (cvSalary.getCurrentSalary()!=null){				
					//CvSalary cvSalary=new CvSalary();
					cvSalary.setCvId(cv.getId());
					cvSalary.setCurrency("RMB");
					//cvSalary.setCurrentSalary(StringHelper.parseInt(companyCc.getSalary(), 0));
					cvSalary.setExpectSalary(0);
					cvSalary.setCreateDt(new Date());
					cvSalary.setCreator(cv.getCreator());			
					cvSalaryDao.save(cvSalary);
					cvSalaryDao.flush();
				}
				
				
				//人选加入
				if (searchProjectCv.getCompanyId()!=null){
					if (searchProjectCv.getCompanyId()>0){
						CompanyCv companyCv=new CompanyCv();
						companyCv.setCompanyId(searchProjectCv.getCompanyId());
						companyCv.setDeptId(searchProjectCv.getDeptId());
						companyCv.setTitleId(searchProjectCv.getTitleId());
						companyCv.setCreateDt(new Date());
						companyCv.setCreator(cv.getCreator());
						companyCv.setCvId(cv.getId());
						companyCvDao.save(companyCv);
						companyCvDao.flush();	
						
						//设置人才的行业为公司的行业
						Company company=(Company)companyService.getById(companyCv.getCompanyId());
						if(company.getIndustry()!=null&&((cv.getIndustry()==null)||cv.getIndustry()!=company.getIndustry())){
							cv.setIndustry(company.getIndustry());
							companyCvDao.update(cv);
							companyCvDao.flush();
						}
						
						//自动并设置cc项目目标公司人选
						searchProjectCvService.addProjectCvByCompanyCv(companyCv.getCompanyId());
					}
				}
				
				
				//人选自动设置客户联系人,如果有
				cvService.doCompanyContacter(cv);
				
				searchProjectService.cvJoinProject(searchProjectCv);
				
				//如果有填写 联系记录  则保存联系记录crm信息
				if(StringUtils.isNotEmpty(searchProjectCcList.getCrmInfo())){
					SearchProjectCvCrm searchProjectCvCrm=new SearchProjectCvCrm();
					searchProjectCvCrm.setCreator(searchProjectCcList.getCreator());
					searchProjectCvCrm.setCrmType(ConstantDic.ContactMethod_C);
					searchProjectCvCrm.setProjectId(searchProjectCcList.getProjectId());
					searchProjectCvCrm.setCvId(cv.getId());
					searchProjectCvCrm.setCreateDt(new Date());
					searchProjectCvCrm.setCrmDt(new Date());
					searchProjectCvCrm.setComment(searchProjectCcList.getCrmInfo());
					cvSalaryDao.save(searchProjectCvCrm);
					cvSalaryDao.flush();
				}
				
			//}
			
			}
		
			//
			

			
		//保存联系记录
		//保存CC
		//如果填写了 CC备注 
		if(StringUtils.isNotEmpty(searchProjectCcList.getComment())){
			if (!StringHelper.isEmpty(searchProjectCcList.getCaller())){
				  searchProjectCcList.setRefCvid(cv.getId());
				  searchProjectCcListDao.save(searchProjectCcList);
				  searchProjectCcListDao.flush();
				}else {
					//只要是通过新建cc人选界面新建的，都要保存searchProjectCcList
					searchProjectCcList.setRefCvid(cv.getId());
					searchProjectCcList.setCallStatus(0);
					searchProjectCcList.setCcResult(0);
					searchProjectCcList.setAccount(searchProjectCcList.getCreator());
					searchProjectCcListDao.save(searchProjectCcList);
					searchProjectCcListDao.flush();	
				}
		}
		
		//searchProjectCvService.updateProjectCvStatus(searchProjectCv)
		SearchProjectCv tempSearchProjectCv=new SearchProjectCv();
	    tempSearchProjectCv.setCvId(cv.getId());
	    tempSearchProjectCv.setProjectId(searchProjectCcList.getProjectId());
	    if (cv.getId()!=null&&cv.getId().intValue()!=0){
			if (!companyService.isLvCv(cv.getId())){	    	
			    //tempSearchProjectCv.setStatus(SearchProjectCv.STATUS_GETCV);		    
			}else{
				//tempSearchProjectCv.setStatus(SearchProjectCv.STATUS_LX);
			}
			tempSearchProjectCv.setStatus(SearchProjectCv.STATUS_LX);
			searchProjectCvService.updateProjectCvStatus(tempSearchProjectCv);
	    }
	  //更新联系方式
		cvService.updateCvContact(cv.getId());
	    
		
	}
	public void update(SearchProjectCcList searchProjectCcList){
		searchProjectCcListDao.update(searchProjectCcList);
		searchProjectCcListDao.flush();
		
		SearchProjectCv tempSearchProjectCv=new SearchProjectCv();
		if (searchProjectCcList.getRefCvid()!=null){
		    tempSearchProjectCv.setCvId(searchProjectCcList.getRefCvid());
		    tempSearchProjectCv.setProjectId(searchProjectCcList.getProjectId());
			if (!companyService.isLvCv(searchProjectCcList.getRefCvid())){	    	
			    //tempSearchProjectCv.setStatus(SearchProjectCv.STATUS_GETCV);		    
			}else{
				//tempSearchProjectCv.setStatus(SearchProjectCv.STATUS_LX);
			}
			tempSearchProjectCv.setStatus(SearchProjectCv.STATUS_LX);
			searchProjectCvService.updateProjectCvStatus(tempSearchProjectCv);
		}
	}
	
	public boolean addCcCheck(Cv cv, List<CvContactDetail> contact_list,
			SearchProjectCcList searchProjectCcList) {
		return searchProjectCcListDao.addCcCheck(cv, contact_list, searchProjectCcList);
	}
	
	
	
	
}
